import Vue from 'vue'
import App from './App'
import axios from 'axios'
import router from './router'
import store from './store/store'
import filters from './filter/filters'
import * as api from '@/api/ajax.js'
import VueI18n from 'vue-i18n'
import messages from './common/language/index'
import {getCookie} from '@/util/util'
import VueLazyload from 'vue-lazyload'
import lazyFilter from './common/js/lazyload'
import MetaInfo from 'vue-meta-info'

// 饿了么组件---start
/*import 'element-ui/lib/theme-chalk/index.css'
import { DatePicker } from 'element-ui'
import lang_en from 'element-ui/lib/locale/lang/en'
import lang_cn from 'element-ui/lib/locale/lang/zh-CN'
import lang_thai from 'element-ui/lib/locale/lang/th'
import locale from 'element-ui/lib/locale'

Vue.use(DatePicker)*/
// 饿了么组件---end

Vue.use(MetaInfo)
Vue.use(VueLazyload, {
  filter: lazyFilter
})
Vue.use(VueI18n)
Vue.config.productionTip = false
Vue.prototype.$http = axios
Vue.prototype.$ajax = api
Vue.use(filters)

// 设置语言
function lang () {
  let t = window.localStorage.getItem('thaiPayMallLanguage')
  if (t) return t
  else return 'thai'
}
const language = lang()
window.lang = lang()
/*if (window.lang === 'cn') {
  locale.use(lang_cn)
} else if (window.lang === 'thai') {
  locale.use(lang_thai)
} else if (window.lang === 'en') {
  locale.use(lang_en)
} else {
  locale.use(lang_thai)
}*/
const i18n = new VueI18n({
  locale: language,    // 语言标识
  messages
})

router.beforeEach((to, from, next) => {
  if (to.meta.requireAuth) {
    if (getCookie('username')) {
      next()
    } else {
      next({
        path: '/login',
        query: {redirect: to.fullPath}
      })
    }
  } else {
    next()
  }
})

/* eslint-disable no-new */
new Vue({
  router,
  el: '#app',
  store,
  i18n,
  template: '<App/>',
  components: { App }
})
